<script setup>
import * as echarts from 'echarts';
import { onMounted, watch } from 'vue'
import { useWindowSize } from '@vueuse/core'

let lineChart
let barChart
let pieChart
let scatterChart

// 折线图渲染函数
const renderLineChart = () => {
  const lineDom = document.querySelector('#line')
  // console.log(lineDom)
  lineChart = echarts.init(lineDom)

  const option1 = {
    title: {
      text: '订单金额的统计图',
      left: 'center'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '金额',
        data: [820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line',
        smooth: true
      },
      {
        name: '库存',
        data: [120, 132, 101, 134, 90, 230, 210],
        type: 'line',
        smooth: true
      }
    ]
  }

  lineChart.setOption(option1)
}

// 渲染柱状图函数
const renderBarChart = () => {
  const barDom = document.querySelector('#bar')
  barChart = echarts.init(barDom)

  const option2 = {
    title: {
      text: '订单销售的柱状图',
      left: 'center'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    tooltip: {
      trigger: 'axis'
    },
    xAxis: {
      type: 'category',
      data: ['水果', '零食', '饮料', '奶制品', '生活用品']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '金额',
        data: [120, 200, 150, 80, 130],
        type: 'bar'
      },
      {
        name: '销量',
        data: [30, 12, 68, 45, 88],
        type: 'bar'  
      }
    ]
  }

  barChart.setOption(option2)
}

// 渲染饼状图函数
const renderPieChart = () => {
  const pieDom = document.querySelector('#pie')
  pieChart = echarts.init(pieDom)

  const option3 = {
    title: {
      text: '订单销售分布图',
      left: 'center'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    tooltip: {
      trigger: 'axis'
    },
    series: [
      {
        name: 'Access From',
        type: 'pie',
        radius: '50%',
        data: [
          { value: 1048, name: 'Search Engine' },
          { value: 735, name: 'Direct' },
          { value: 580, name: 'Email' },
          { value: 484, name: 'Union Ads' },
          { value: 300, name: 'Video Ads' }
        ],
        emphasis: {
          itemStyle: {
            shadowBlur: 10,
            shadowOffsetX: 0,
            shadowColor: 'rgba(0, 0, 0, 0.5)'
          }
        }
      }
    ]
  }

  pieChart.setOption(option3)
}

// 渲染散点图函数
const renderScatterChart = () => {
  const scatterDom = document.querySelector('#scatter')
  scatterChart = echarts.init(scatterDom)

  const option4 = {
    title: {
      text: '订单销售分步图',
      left: 'center'
    },
    tooltip: {
      trigger: 'axs'
    },
    legend: {
      orient: 'vertical',
      left: 'left'
    },
    xAxis: {},
    yAxis: {},
    series: [
      {
        symbolSize: 20,
        data: [
          [10.0, 8.04],
          [8.07, 6.95],
          [13.0, 7.58],
          [9.05, 8.81],
          [11.0, 8.33],
          [14.0, 7.66],
          [13.4, 6.81],
          [10.0, 6.33],
          [14.0, 8.96],
          [12.5, 6.82],
          [9.15, 7.2],
          [11.5, 7.2],
          [3.03, 4.23],
          [12.2, 7.83],
          [2.02, 4.47],
          [1.05, 3.33],
          [4.05, 4.96],
          [6.03, 7.24],
          [12.0, 6.26],
          [12.0, 8.84],
          [7.08, 5.82],
          [5.02, 5.68]
        ],
        type: 'scatter'
      }
    ]
  }

  scatterChart.setOption(option4)
}

onMounted(() => {
  renderLineChart()
})

onMounted(() => {
  renderBarChart()
})

onMounted(() => {
  renderPieChart()
})

onMounted(() => {
  renderScatterChart()
})

// 利用vueUse给出视口的变化,并监听视口，一旦发生变化重新渲染echarts
// 窗口大小变化时的处理函数
function resizeChart(dom) {
    dom.resize();
}
const { width, height } = useWindowSize()
watch([width, height], () => {
  resizeChart(lineChart)
  resizeChart(barChart)
  resizeChart(pieChart)
  resizeChart(scatterChart)
})
</script>

<template>
  <el-row :gutter="10" style="margin-bottom: 20px;">
    <el-col :span="12">
      <!-- 折线图 -->
      <el-card>
        <div style="width: 100%; height: 400px;" id="line"></div>
      </el-card>
    </el-col>
    <el-col :span="12">
      <el-card>
        <!-- 柱状图 -->
        <div style="width: 100%; height: 400px;" id="bar"></div>
      </el-card>
    </el-col>
  </el-row>
  <el-row :gutter="10">
    <el-col :span="12">
      <!-- 饼状图 -->
      <el-card>
        <div style="width: 100%; height: 400px;" id="pie"></div>
      </el-card>
    </el-col>
    <el-col :span="12">
      <el-card>
        <!-- 散点图 -->
        <div style="width: 100%; height: 400px;" id="scatter"></div>
      </el-card>
    </el-col>
  </el-row>
</template>

<style scoped lang='scss'>
</style>